package jinyilw.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Random;

public class TextTools
{
	private static String lineStr;

	private TextTools()
	{
	}

	public static String lineString()
	{
		if (lineStr == null)
			lineStr = System.lineSeparator();
		return lineStr;
	}

	public static String getFileExt(String filename)
	{
		if ((filename != null) && (filename.length() > 1))
		{
			int dot = filename.lastIndexOf('.') + 1;
			if (dot == 0 || dot == filename.length())
				return null;
			return filename.substring(dot);
		}
		return null;
	}

	public static String getFileNameNoExt(String filename)
	{
		if ((filename != null) && (!filename.isEmpty()))
		{
			int dot = filename.lastIndexOf('.');
			if (dot > -1)
				return filename.substring(0, dot);
		}
		return filename;
	}

	public static String getFileName(String filePath)
	{
		if ((filePath != null) && (!filePath.isEmpty()))
		{
			int index = filePath.lastIndexOf(File.separatorChar) + 1;
			if ((index > 0) && (index < filePath.length()))
				return filePath.substring(index);
		}
		return filePath;
	}

	public static String readUTF8TxtFile(File f)
	{
		return readTxtFile(f, StandardCharsets.UTF_8);
	}

	public static String readGBKTxtFile(File f)
	{
		return readTxtFile(f, Charset.forName("GBK"));
	}

	public static String readTxtFile(File f, Charset cs)
	{
		if (f == null)
			return null;
		BufferedReader b = null;
		try
		{
			b = new BufferedReader(
					new InputStreamReader(new FileInputStream(f), cs));
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		return readText(b);
	}

	public static String readText(Reader reader)
	{
		if (reader == null)
			return null;
		StringBuilder sb = new StringBuilder();
		String str;
		try
		{
			char[] data = new char[1024];
			int rn;
			while ((rn = reader.read(data)) > 0)
			{
				str = String.valueOf(data, 0, rn);
				sb.append(str);
			}
			reader.close();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return sb.toString();
	}

	public static void writeTextFile(File f, String s)
	{
		FileWriter fileWriter;
		try
		{
			fileWriter = new FileWriter(f);
			fileWriter.write(s.toCharArray());
			fileWriter.close();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public static void writeTextFile(File f, String s, Charset cs)
	{
		if (f == null || s == null)
			return;
		OutputStreamWriter out;
		try
		{
			out = new OutputStreamWriter(new FileOutputStream(f), cs);
			out.write(s.toCharArray());
			out.close();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public static void writeUTF8TextFile(File f, String s)
	{
		writeTextFile(f, s, StandardCharsets.UTF_8);
	}

	public static void gbkToUtf8(File srcFile, File destFile)
	{
		if (srcFile == null || destFile == null)
			return;
		String s = readGBKTxtFile(srcFile);
		writeUTF8TextFile(destFile, s);
	}

	// 随机生成常见汉字
	public static String getRandomChar()
	{
		String str = "";
		int highCode;
		int lowCode;
		Random random = new Random();
		highCode = (176 + Math.abs(random.nextInt(39))); // B0 + 0~39(16~55)
		// 一级汉字所占区
		lowCode = (161 + Math.abs(random.nextInt(93))); // A1 + 0~93 每区有94个汉字
		byte[] b = new byte[2];
		b[0] = (Integer.valueOf(highCode)).byteValue();
		b[1] = (Integer.valueOf(lowCode)).byteValue();
		try
		{
			str = new String(b, "GBK");
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		return str;
	}

	public static boolean isBlank(String s)
	{
		if (s == null)
			return true;
		for (int i = 0, l = s.length(); i < l; i++)
			if (s.charAt(i) != ' ')
				return false;
		return true;
	}

	public static boolean isNumber(String s)
	{
		int l = s.length();
		if (l == 0)
			return false;
		for (int i = 0; i < l; i++)
			if (!Character.isDigit(s.charAt(i)))
				return false;
		return true;
	}
}
